/*
 * Copyright (C) 2011, EADS France
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package xowl.interpreter.frontends.owlxml;

import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class RuleLoader {
    public static xowl.lang.rules.Rule load(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        String name = node.getNodeName();
        xowl.interpreter.frontends.LexicalContext innerContext = new xowl.interpreter.frontends.LexicalContext(context);
        if (name.equals(xowl.interpreter.frontends.VocRules.ruleRule)) return load_Rule(ontologyLoader, node, innerContext);
        return null;
    }

    public static xowl.lang.rules.Rule load_Rule(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        xowl.lang.rules.Rule rule = new xowl.lang.rules.Rule();
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        rule.setHasIRI(ontologyLoader.getFullIRI(children.get(0)));
        for (int i=1; i!=children.size(); i++) {
            String name = children.get(i).getNodeName();
            if (name.equals(xowl.interpreter.frontends.VocRules.ruleAntecedents)) {
                for (int j=0; j!=children.get(i).getChildNodes().getLength(); j++) {
                    org.w3c.dom.Node child = children.get(i).getChildNodes().item(j);
                    rule.addAntecedents(load_Assertion(ontologyLoader, child, context));
                }
            } else if (name.equals(xowl.interpreter.frontends.VocRules.ruleConsequents)) {
                for (int j=0; j!=children.get(i).getChildNodes().getLength(); j++) {
                    org.w3c.dom.Node child = children.get(i).getChildNodes().item(j);
                    rule.addConsequents(load_Assertion(ontologyLoader, child, context));
                }
            } else if (name.equals(xowl.interpreter.frontends.VocRules.ruleGuards)) {
                for (int j=0; j!=children.get(i).getChildNodes().getLength(); j++) {
                    org.w3c.dom.Node child = children.get(i).getChildNodes().item(j);
                    xowl.lang.owl2.LiteralExpression exp = ExpressionLoader.loadLiteralExp(ontologyLoader, child, context);
                    if (exp != null)
                        rule.addGuards(exp);
                }
            }
        }
        return rule;
    }
    
    public static xowl.lang.rules.Assertion load_Assertion(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        String name = node.getNodeName();
        if (name.equals(xowl.interpreter.frontends.VocActions.expNot)) {
            xowl.lang.rules.Assertion assertion = load_Assertion_Inner(ontologyLoader, node, context);
            assertion.setIsPositive(!assertion.getIsPositive());
            return assertion;
        } else if (name.equals(xowl.interpreter.frontends.VocRules.ruleMeta)) {
            xowl.lang.rules.Assertion assertion = load_Assertion_Inner(ontologyLoader, node, context);
            assertion.setIsMeta(!assertion.getIsMeta());
            return assertion;
        } else {
            xowl.lang.owl2.Axiom axiom = AxiomLoader.load(ontologyLoader, node, context);
            xowl.lang.rules.Assertion assertion = new xowl.lang.rules.Assertion();
            assertion.addAxioms(axiom);
            assertion.setIsPositive(true);
            assertion.setIsMeta(false);
            return assertion;
        }
    }
    
    private static xowl.lang.rules.Assertion load_Assertion_Inner(OntologyLoader ontologyLoader, org.w3c.dom.Node node, xowl.interpreter.frontends.LexicalContext context) {
        List<org.w3c.dom.Node> children = ExpressionLoader.getElements(node);
        if (children.size() == 1) {
            org.w3c.dom.Node first = children.get(0);
            String name = first.getNodeName();
            if (name.equals(xowl.interpreter.frontends.VocActions.expNot))
                return load_Assertion(ontologyLoader, first, context);
            else if (name.equals(xowl.interpreter.frontends.VocRules.ruleMeta))
                return load_Assertion(ontologyLoader, first, context);
        }
        xowl.lang.rules.Assertion assertion = new xowl.lang.rules.Assertion();
        assertion.setIsPositive(true);
        assertion.setIsMeta(false);
        for (org.w3c.dom.Node child : children)
            assertion.addAxioms(AxiomLoader.load(ontologyLoader, child, context));
        return assertion;
    }
}
